package com.taptech.doufu.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.bean.ComicRecommendGsonBean;
import com.taptech.doufu.bean.ComicRecommendModel;
import com.taptech.doufu.bean.NovelRecommendGsonBean;
import com.taptech.doufu.bean.choice.BannerModel;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.search.SearchStoryView;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.adapter.GuidePagerAdapter;
import com.taptech.doufu.ui.adapter.SearchHistoryAdapter;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.GroupInfoBean;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpRequestObject;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.search.RecommendViewGroup;
import com.taptech.doufu.ui.view.search.SearchBaseView;
import com.taptech.doufu.ui.view.search.SearchContentView;
import com.taptech.doufu.ui.view.search.SearchTagView;
import com.taptech.doufu.ui.view.search.SearchUserView;
import com.taptech.doufu.ui.view.tablayout.TabLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@SuppressLint("NewApi")
public class HomeSearchActivity extends DiaobaoBaseActivity implements HttpResponseListener,View.OnLayoutChangeListener {
	private EditText articleSearch;
	private String commendNovelKey = "请输入作品、关键字或用户昵称"; //搜索框中小说推荐词
	private String searchHint = "请输入作品、关键字或用户昵称";
	private BannerModel commendNovelBanner;
	private TTHomeViewPager mViewPager;
	private View activityRootView;
	private LinearLayout llHistory;
	public static final String SP_SEARCH_HISTORY_LIST = "sp_search_history_list";

	public static final int HISTORY_COUNT = 20;

	//屏幕高度
	private int screenHeight = 0;
	//软件盘弹起后所占高度阀值
	private int keyHeight = 0;

	private ListView historyListView;
	private SearchHistoryAdapter historyAdapter;
	private List<String> historyList;
	// 内容搜索,用户搜索
	private View viewArticle, viewUser, viewTag, viewStory;

	private SearchBaseView searchBaseView[] = new SearchBaseView[4];
	private View del;

	private int oldPosition;
	private List<GroupInfoBean> attentionCircleList;
	private List<GroupInfoBean> storyRecommendList;
	private RecommendViewGroup recommendViewGroup;// 推荐词
	private LinearLayout layRecommendContent; //
	private View recommendLayout;
	private View llPage;
	private TabLayout layTab;
	private float downX,downY;
	private ImageView ivRecommendBanner;
	private List<ComicRecommendModel.Item> comicRecommendList;
	private int initIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_search);
		if (getIntent().hasExtra(Constant.POSITION)) {
			initIndex = getIntent().getIntExtra(Constant.POSITION,0);
		}
		//获取屏幕高度
		screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
		//阀值设置为屏幕高度的1/3
		keyHeight = screenHeight/3;
		initView();
		getNovelRecommend();
		getStoryRecommend();
	}

	private void initView() {

		layTab = (TabLayout) findViewById(R.id.layTab);
		layTab.setTabTextColors(getResources().getColor(R.color.text_color_7),getResources().getColor(R.color.text_color_17));
		layTab.setSelectedTabIndicatorColor(getResources().getColor(R.color.text_color_17));
		layTab.setTabMode(TabLayout.MODE_FIXED);
		ivRecommendBanner = (ImageView) findViewById(R.id.ivRecommendBanner);
		int bannerWidth = ScreenUtil.getScreenWidth(this)-ScreenUtil.dip2px(this,30);
		int bannerHeight = bannerWidth/4;
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(bannerWidth, bannerHeight);
		params.leftMargin = ScreenUtil.dip2px(this,15);
		ivRecommendBanner.setLayoutParams(params);
		ivRecommendBanner.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ChoiceUtils.clickBanner(HomeSearchActivity.this,commendNovelBanner,"");
			}
		});


		articleSearch = (EditText) findViewById(R.id.search_activity_article_text);
		articleSearch.setHint(commendNovelKey);
		llHistory = (LinearLayout) findViewById(R.id.llHistory);
		llPage = findViewById(R.id.llPage);
		hideHistory();
		historyListView = (ListView) findViewById(R.id.historyListView);
		activityRootView = findViewById(R.id.activityRootView);
		del = findViewById(R.id.search_activity_article_del);
		recommendViewGroup = (RecommendViewGroup) findViewById(R.id.search_activity_article_recommend);
		recommendLayout = findViewById(R.id.search_recommend_layout);
		layRecommendContent = findViewById(R.id.layRecommendContent);

		recommendLayout.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()){
					case MotionEvent.ACTION_DOWN:
						downX = event.getX();
						downY = event.getY();
						break;
					case MotionEvent.ACTION_MOVE:

						break;
					case MotionEvent.ACTION_UP:
						if (Math.abs(downX-event.getX())>Math.abs(downY-event.getY()) &&
								Math.abs(downX-event.getX())>60f){
							int curIndex = mViewPager.getCurrentItem();
							if (downX < event.getX()) {
								//向左滑动
								mViewPager.setCurrentItem(curIndex-1);
							}else{
								//向右滑动
								mViewPager.setCurrentItem(curIndex+1);
							}
						}
						break;
				}
				return true;
			}
		});

		articleSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (!TextUtils.isEmpty(s.toString())) {
					del.setVisibility(View.VISIBLE);
				} else {
					recommendLayout.setVisibility(View.VISIBLE);
					del.setVisibility(View.GONE);
				}
				searchBaseView[0].hideFilter(true);
			}
		});

		mViewPager = (TTHomeViewPager) findViewById(R.id.search_activity_viewpager);
		mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
		LayoutInflater mLi = LayoutInflater.from(this);
		// 内容搜索
		viewArticle = mLi.inflate(R.layout.home_search_article_layout, null);
		viewStory = mLi.inflate(R.layout.home_search_story_layout, null);
		viewTag = mLi.inflate(R.layout.activity_tags_collect_list, null);
		viewUser = mLi.inflate(R.layout.home_search_user_layout, null);

		final ArrayList<View> views = new ArrayList<View>();
		views.add(viewArticle);
		views.add(viewStory);
		views.add(viewTag);
		views.add(viewUser);
		List<String> titleList = new ArrayList<>();
		titleList.add("小说");
		titleList.add("小故事");
		titleList.add("标签");
		titleList.add("用户");
		// 填充ViewPager的数据适配器
		GuidePagerAdapter mPagerAdapter = new GuidePagerAdapter(views,titleList);
		mViewPager.setAdapter(mPagerAdapter);
		layTab.setupWithViewPager(mViewPager);
		searchBaseView[0] = new SearchContentView(HomeSearchActivity.this, viewArticle, true);

		articleSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH){
					searchContent(false);
					return true;
				}
				return false;
			}
		});
		mViewPager.setCurrentItem(initIndex);
		initSearchHistory();
	}


	private void initSearchHistory() {
		historyList = new ArrayList<>();
		List<String> tempList = DiaoBaoSharedPreferences.getDataList(this, SP_SEARCH_HISTORY_LIST);
		if (tempList != null && tempList.size() > 0) {
			historyList = tempList;
		}
		historyAdapter = new SearchHistoryAdapter(historyList, new SearchHistoryAdapter.OnListListen() {
			@Override
			public void onListChange(List<String> list) {
				historyList = list;
				if (historyList.size() == 0) {
					hideHistory();
				}
			}

			@Override
			public void onClick(String key) {
				if (articleSearch != null) {
					articleSearch.setText(key);
					articleSearch.setSelection(articleSearch.length());
					searchContent(false);
				}
			}
		});
		historyListView.setAdapter(historyAdapter);
		View footView = LayoutInflater.from(this).inflate(R.layout.footview_search_history, null);
		footView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (historyList != null) {
					historyList.clear();
					hideHistory();
				}
			}
		});
		historyListView.addFooterView(footView);
	}

	private void showHistory() {
		llHistory.setVisibility(View.VISIBLE);
		llPage.setVisibility(View.GONE);
	}

	private void hideHistory() {
		llHistory.setVisibility(View.GONE);
		llPage.setVisibility(View.VISIBLE);
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (historyList != null) {
			DiaoBaoSharedPreferences.setDataList(this,SP_SEARCH_HISTORY_LIST,historyList);
		}
	}

	String searchContent;

	@Override
	public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
		//现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
		if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
			if (historyList != null && historyList.size() > 0) {
				showHistory();
			}else{
				hideHistory();
			}
		}else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
			hideHistory();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		//添加layout大小发生改变监听器
		activityRootView.addOnLayoutChangeListener(this);
	}

	public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

		public void onPageSelected(int page) {
			articleSearch.setHint(searchHint);
			// 翻页时当前page,改变当前状态园点图片
			switch (page) {
				case 0:
					searchBaseView[0].hideFilter(false);
					articleSearch.setHint(commendNovelKey);
					updateTagRecommend();
					layRecommendContent.setVisibility(View.VISIBLE);
					break;
				case 1:
					if (searchBaseView[1] == null) {
						searchBaseView[1] = new SearchStoryView(HomeSearchActivity.this, viewStory);
					}
//					getComicsRecommend();
					updateStoryTagRecommend();
					layRecommendContent.setVisibility(View.VISIBLE);
					break;
				case 2:
					if (searchBaseView[2] == null) {
						searchBaseView[2] = new SearchTagView(HomeSearchActivity.this, viewTag);
					}
					layRecommendContent.setVisibility(View.GONE);
					break;
				case 3:
					if (searchBaseView[3] == null) {
						searchBaseView[3] = new SearchUserView(HomeSearchActivity.this, viewUser);
					}
					layRecommendContent.setVisibility(View.GONE);
					break;

				default:
					updateTagRecommend();

					break;
			}
			searchContent = articleSearch.getText().toString();
			if (!("".equals(searchContent))) {
				searchBaseView[page].searchContent(searchContent);
			}
			oldPosition = page;
			showCommendNovelBanner();
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	public void handleResponse(int handleType, HttpResponseObject response) {

	}


	public void searchArticleOnclick(View v) {
		isShowTagView();
		mViewPager.setCurrentItem(0);
	}

	public void searchCartoonOnclik(View view) {
		isShowTagView();
		mViewPager.setCurrentItem(1);
	}

	public void searchTagOnclik(View view) {
		isShowTagView();
		mViewPager.setCurrentItem(2);
	}

	public void searchUserOnclick(View v) {
		isShowTagView();
		mViewPager.setCurrentItem(3);
	}

	public void isShowTagView(){
		String content = articleSearch.getText().toString();
		if(StringUtil.stringLength(content)){
			recommendLayout.setVisibility(View.GONE);
		}
	}
	public void searchOnclick(View v) {
		searchContent(false);
	}

	private void searchContent(boolean isHotTag){
		int type = mViewPager.getCurrentItem();
		String content = articleSearch.getText().toString();
		if ("".equals(content)) {
			if (TextUtils.isEmpty(articleSearch.getHint())) {
				UIUtil.toastMessage(this, "搜索的内容不为空");
				return;
			}else{
				if (searchHint.equals(articleSearch.getHint().toString())) {
					UIUtil.toastMessage(this, "搜索的内容不为空");
					return;
				}else{
					content = articleSearch.getHint().toString();
					articleSearch.setText(content);
				}
			}
		}
		addHistory(content);
		DiaobaoUtil.hideInputManager(HomeSearchActivity.this);
		recommendLayout.setVisibility(View.GONE);
		StringBuilder eventName = new StringBuilder();
		if (isHotTag) {
			eventName.append("hotkeywork-");
		}else{
			eventName.append("search-");
		}
		if (type == 0){
			eventName.append("小说");
		} else if (type == 1) {
			eventName.append("漫画");
		} else if (type == 2) {
			eventName.append("标签");
		} else if (type == 3) {
			eventName.append("用户");
		}
		UserLogManager.addLogBySearch(eventName.toString(),content);
		searchBaseView[type].searchContent(content);
	}

	/**
	 * 添加搜索数据
	 * @param searchContent
	 */
	private void addHistory(String searchContent) {
		if (historyList != null) {
			boolean exit = historyList.contains(searchContent);
			if (exit) {
				for(int i=0;i<historyList.size();i++) {
					String key = historyList.get(i);
					if (searchContent.equals(key)) {
						historyList.remove(i);
						break;
					}
				}
			}
			historyList.add(0,searchContent);
			if(historyList.size()>HISTORY_COUNT){
				historyList.remove(historyList.size() - 1);
			}
		}
	}

	public void back(View view) {
		onBackPressed();
	}


	public void clean(View view) {
		articleSearch.setText("");
	}


	public void getRecommend() {
		new Thread() {
			@Override
			public void run() {
				HttpURLConnection urlConnection = null;
				try {
					URL url = new URL(Constant.BASE_URL + "search/get_popular_keywords?size=" + Constant.PAGE_SIZE_10 + "&type=3");
					urlConnection = (HttpURLConnection) url.openConnection();
					urlConnection.setConnectTimeout(5000);
					urlConnection.setRequestMethod("GET");
					InputStream in = new BufferedInputStream(urlConnection.getInputStream());
					String result = readInStream(in);
					try {
						JSONObject jsonObject = new JSONObject(result);
						setResultValue(jsonObject);
					} catch (Exception e) {
						e.printStackTrace();
					}

				} catch (MalformedURLException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					urlConnection.disconnect();
				}
				super.run();
			}
		}.start();
	}

	private void setResultValue(JSONObject jsonObj) throws Exception {
		{
			HttpResponseObject res = new HttpResponseObject();
			if (jsonObj == null) {
				res.setStatus(Constant.STATUS_TIME_OUT);
				return;
			}
			res.setData(jsonObj.get("data"));
			res.setFail_code(jsonObj.getInt("fail_code"));
			res.setStatus(jsonObj.getInt("status"));
			res.setTime_begin(jsonObj.getString("time_begin"));
			res.setTime_cost(jsonObj.getString("time_cost"));
			res.setUser_msg(DiaobaoUtil.getStringFromJSONObject(jsonObj, "user_msg"));
			if (jsonObj.has("meta"))
				res.setMeta(jsonObj.getString("meta"));

			HttpRequestObject request = new HttpRequestObject();
			request.setHandleType(4004);
			request.setUrl(Constant.BASE_URL + "search/get_popular_keywords?size="
					+ Constant.PAGE_SIZE_10 + "&type=3");
			request.setListener(HomeSearchActivity.this);

			if (request.getListener() != null) {
				Message msg = mHandler.obtainMessage();
				msg.obj = res;
				mHandler.sendMessage(msg);
			}
		}
	}

	private String readInStream(InputStream in) {
		Scanner scanner = new Scanner(in).useDelimiter("\\A");
		return scanner.hasNext() ? scanner.next() : "";
	}


	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				HttpResponseObject response = (HttpResponseObject) msg.obj;
				JSONArray obj = (JSONArray) response.getData();
				attentionCircleList = new ArrayList<>();
				for (int i = 0; i < obj.length(); i++) {
					GroupInfoBean bean = new GroupInfoBean();
					bean.setData(obj.getString(i));
					attentionCircleList.add(bean);
				}
				updateTagRecommend();

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};


	private void updateTagRecommend() {
		if (attentionCircleList!=null){
			recommendViewGroup.removeAllViews();
			for (int i = 0; i < attentionCircleList.size(); i++) {
				final int pos = i;
				TextView text = new TextView(HomeSearchActivity.this);
				text.setClickable(true);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
				text.setLayoutParams(params);
				text.setTextSize(13);
				text.setText(attentionCircleList.get(i).getData());
				text.setGravity(Gravity.CENTER);
				text.setTextColor(getTagTextColor());
				text.setBackgroundResource(getTagBg());
				text.setPadding(ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6), ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6));
				text.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						String searchContent =   attentionCircleList.get(pos).getData();
						articleSearch.setText(searchContent);
						articleSearch.setSelection(articleSearch.length());
						searchContent(true);

					}
				});
				recommendViewGroup.addView(text);
			}
		}
	}



	private void updateStoryTagRecommend() {
		if (storyRecommendList !=null){
			recommendViewGroup.removeAllViews();
			for (int i = 0; i < storyRecommendList.size(); i++) {
				final int pos = i;
				TextView text = new TextView(HomeSearchActivity.this);
				text.setClickable(true);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
				text.setLayoutParams(params);
				text.setTextSize(13);
				text.setText(storyRecommendList.get(i).getData());
				text.setGravity(Gravity.CENTER);
				text.setTextColor(getTagTextColor());
				text.setBackgroundResource(getTagBg());
				text.setPadding(ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6), ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6));
				text.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						String searchContent =   storyRecommendList.get(pos).getData();
						articleSearch.setText(searchContent);
						articleSearch.setSelection(articleSearch.length());
						searchContent(true);

					}
				});
				recommendViewGroup.addView(text);
			}
		}
	}



	private void getComicsRecommend() {
		if (comicRecommendList != null && comicRecommendList.size() > 0) {
			updateComicsRecommend();
		}else{
			ApiClient.getInstance().setUseCache(true).getService().getComicRecommendInSearch()
					.compose(RxJavaHelper.<ComicRecommendGsonBean>observeOnMainThread(this))
					.subscribe(new BaseSubscriber<ComicRecommendGsonBean>(){
						@Override
						public void onNext(ComicRecommendGsonBean comicRecommendGsonBean) {
							super.onNext(comicRecommendGsonBean);
							if (comicRecommendGsonBean != null &&
									comicRecommendGsonBean.getData() != null &&
									comicRecommendGsonBean.getData().getList() != null &&
									comicRecommendGsonBean.getData().getList().size() > 0) {
								comicRecommendList = comicRecommendGsonBean.getData().getList();
								updateComicsRecommend();
							}
						}
					});
		}
	}


	private void getNovelRecommend() {
		ApiClient.getInstance().setUseCache(true).getService().getNovelRecommendInSearch(3,0, Constant.PAGE_SIZE_10)
				.compose(RxJavaHelper.<NovelRecommendGsonBean>observeOnMainThread(this))
				.subscribe(new BaseSubscriber<NovelRecommendGsonBean>(){
					@Override
					public void onNext(NovelRecommendGsonBean bean) {
						super.onNext(bean);
						if (bean != null && bean.getData() != null) {
							if (bean.getData().getList()!=null){
								attentionCircleList = new ArrayList<>();
								for (int i = 0; i < bean.getData().getList().size(); i++) {
									GroupInfoBean model = new GroupInfoBean();
									model.setData(bean.getData().getList().get(i));
									attentionCircleList.add(model);
								}
								updateTagRecommend();
							}
							if (bean.getData().getNovel_placeholder() != null &&
									bean.getData().getNovel_placeholder().size() > 0) {
								commendNovelKey = bean.getData().getNovel_placeholder().get(0);
								articleSearch.setHint(commendNovelKey);
							}
							if (bean.getData().getBanner() != null && bean.getData().getBanner().size() > 0) {
								commendNovelBanner = bean.getData().getBanner().get(0);
								showCommendNovelBanner();
							}

						}
					}
				});
	}

	private void getStoryRecommend() {
		ApiClient.getInstance().setUseCache(true).getService().getNovelRecommendInSearch(3,1, Constant.PAGE_SIZE_10)
				.compose(RxJavaHelper.<NovelRecommendGsonBean>observeOnMainThread(this))
				.subscribe(new BaseSubscriber<NovelRecommendGsonBean>(){
					@Override
					public void onNext(NovelRecommendGsonBean bean) {
						super.onNext(bean);
						if (bean != null && bean.getData() != null) {
							if (bean.getData().getList()!=null){
								storyRecommendList = new ArrayList<>();
								for (int i = 0; i < bean.getData().getList().size(); i++) {
									GroupInfoBean model = new GroupInfoBean();
									model.setData(bean.getData().getList().get(i));
									storyRecommendList.add(model);
								}
								if (initIndex == 1) {
									updateStoryTagRecommend();
								}
							}
						}
					}
				});
	}

	private void showCommendNovelBanner() {
		if (commendNovelBanner != null && mViewPager.getCurrentItem() == 0) {
			ivRecommendBanner.setVisibility(View.VISIBLE);
			GlideUtil.displayImage(ivRecommendBanner,commendNovelBanner.getBanner_img());
		}else{
			ivRecommendBanner.setVisibility(View.GONE);
		}
	}


	private void updateComicsRecommend() {
		recommendViewGroup.removeAllViews();
		for (int i = 0; i < comicRecommendList.size(); i++) {
			final int pos = i;
			TextView text = new TextView(HomeSearchActivity.this);
			text.setClickable(true);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			text.setLayoutParams(params);
			text.setTextSize(13);
			text.setText(comicRecommendList.get(i).getTitle());
			text.setGravity(Gravity.CENTER);
			text.setTextColor(getTagTextColor());
			text.setBackgroundResource(getTagBg());
			text.setPadding(ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6), ScreenUtil2.dip2px(15), ScreenUtil2.dip2px(6));
			text.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String searchContent =   (comicRecommendList.get(pos).getTitle());
					articleSearch.setText(searchContent);
					articleSearch.setSelection(articleSearch.length());
					searchContent(true);

				}
			});
			recommendViewGroup.addView(text);
		}
	}

	private int getTagBg() {
		if (WeMediaApplication.getInstance().isDayNightMode_Night) {
			return R.drawable.radius_attention_circle_gray_dark;
		}else{
			return R.drawable.radius_attention_circle_gray;
		}
	}

	private int getTagTextColor() {
		if (WeMediaApplication.getInstance().isDayNightMode_Night) {
			return ContextCompat.getColor(this,R.color.text_dark);
		}else{
			return ContextCompat.getColor(this, R.color.text_color_8);
		}
	}
}